package ${groupId};

import java.io.IOException;
import java.net.InetAddress;
import java.time.Duration;
import java.time.LocalDateTime;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootVersion;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ApplicationListener;
import org.springframework.core.SpringVersion;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;

import ${groupId}.Application;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @author jianglu
 * @see ComponentScan使用 https://blog.csdn.net/onedaycbfly/article/details/79082730
 */
@SpringBootApplication(scanBasePackages= {"${groupId}","com.system"})
@MapperScan(basePackages = {"${groupId}.dal.mybatis.mapper","${groupId}.dal.mybatis.mymapper","com.system.**.mapper"})
@EnableAsync
@ServletComponentScan
@Slf4j
public class Application  implements ApplicationListener<ApplicationReadyEvent>{
	
	@Autowired
	private Environment environment;
	
	public static void main(String[] args) throws Exception {
		LocalDateTime startTime = LocalDateTime.now();
		SpringApplication.run(Application.class, args);
		LocalDateTime endTime = LocalDateTime.now();
		Duration duration= Duration.between(startTime, endTime );
		log.info("服务器启动耗时: {}.{}秒",duration.toSecondsPart(),duration.toMillisPart());
    }
	
	@Override
	public void onApplicationEvent(ApplicationReadyEvent event) {
        try {
            StringBuilder sb = new StringBuilder("\n/--------------------------------------------------------------\\\n");
    		sb.append("| 版本Springframework Version: " + SpringVersion.getVersion()+"\n");
    		sb.append("| SpringBoot Version: " + SpringBootVersion.getVersion()+"\n");
    		sb.append("| 操作系统: " + System.getProperty("os.name")+"\n");
    		sb.append("| 服务器主机: " + InetAddress.getLocalHost().getHostAddress()+"("+InetAddress.getLocalHost().getHostName()+")"+"\n");
    		sb.append("| 服务器端口: " + environment.getProperty("server.port")+"\n");
    		sb.append("| 日志目录: " + environment.getProperty("logging.file.path")+"\n");
    		sb.append("| 运行环境: " + environment.getActiveProfiles()[0]+"运行环境\n");
    		sb.append("| 数据库连接地址: " + environment.getProperty("spring.datasource.url")+"\n");
    		sb.append("| 数据库连接池大小: " + environment.getProperty("spring.datasource.druid.max-active")+"个\n");
    		sb.append("| 文档地址: http://127.0.0.1:" + environment.getProperty("server.port")+"/doc.html\n");
    		sb.append("\\--------------------------------------------------------------/");
    		log.info(sb.toString());
			Runtime.getRuntime().exec("cmd /c start http://127.0.0.1:"+environment.getProperty("server.port")+"/boss/login");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}